package com.booking.springrestful.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.springrestful.domain.Consultationroom;
import com.booking.springrestful.domain.Timeslot;
import com.booking.springrestful.exception.BusinessException;
import com.booking.springrestful.mapper.ConsultationroomMapper;
import com.booking.springrestful.mapper.TimeslotMapper;
import com.booking.springrestful.resp.ResultCodeEnum;
import com.booking.springrestful.service.TimeslotService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 13182
* @description 针对表【timeslot(咨询室时间安排)】的数据库操作Service实现
* @createDate 2024-11-07 21:32:01
*/
@Service
public class TimeslotServiceImpl extends ServiceImpl<TimeslotMapper, Timeslot>
    implements TimeslotService {


    @Resource
    private TimeslotMapper timeslotMapper;

    @Resource
    private ConsultationroomMapper consultationroomMapper;

    @Override
    public void addTimeslot(List<Timeslot> list) {
        check(list);

        int num=0;
        // 如果没有抛出异常，那么可以安全地插入这些时间段
        for (Timeslot timeslot : list) {
            timeslotMapper.insert(timeslot);
            num++;
        }
        Consultationroom consultationroom = new Consultationroom();
        consultationroom.setTimetotal(num);
        consultationroomMapper.update(consultationroom,new QueryWrapper<Consultationroom>().eq("consultationRoomId",list.get(0).getConsultationRoomId()));
    }

    private static void check(List<Timeslot> list) {
        //long thirtyMinutesInMillis = 30 * 60 * 1000L;
        long twoHoursInMillis = 4 * 60 * 60 * 1000L;

        for (Timeslot timeslot : list) {
            Date startTime = timeslot.getStartTime();
            Date endTime = timeslot.getEndTime();
            if (startTime==null||endTime==null){
                throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR, "请填写完整");
            }

            // 检查开始时间是否大于结束时间
            if (startTime.after(endTime)) {
                throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR, "开始时间不能大于结束时间");
            }

            // 检查时间段长度是否在30分钟到2小时之间
            long duration = endTime.getTime() - startTime.getTime();
            if (duration < twoHoursInMillis ) {
                throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR, "时间段不能小于4小时");
            }
        }

        // 检查时间段之间是否有重叠
        for (int i = 0; i < list.size(); i++) {
            Timeslot firstSlot = list.get(i);
            Date firstStart = firstSlot.getStartTime();
            Date firstEnd = firstSlot.getEndTime();

            for (int j = i + 1; j < list.size(); j++) {
                Timeslot secondSlot = list.get(j);
                Date secondStart = secondSlot.getStartTime();
                Date secondEnd = secondSlot.getEndTime();

                // 检查两个时间段是否有重叠
                if (!(firstEnd.before(secondStart) || secondEnd.before(firstStart))) {
                    throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR, "时间段重叠");
                }
            }
        }
    }

    @Override
    public List<Timeslot> getTimeslotById(Integer id) {
        List<Timeslot> timeslot = timeslotMapper.selectList(new QueryWrapper<Timeslot>().eq("consultationRoomId", id));
        return timeslot;
    }

    @Override
    public void updateTimeslot(List<Timeslot> list, Integer consultationRoomId) {

        check(list);

        QueryWrapper<Timeslot> wrapper = new QueryWrapper<Timeslot>().eq("consultationRoomId", consultationRoomId);
        //先删除所有数据
        timeslotMapper.delete(wrapper);

        int num=0;
        if (list.size()!=0){
            for (Timeslot timeslot : list) {
                timeslotMapper.insert(timeslot);
                num++;
            }
        }

        Consultationroom consultationroom = new Consultationroom();
        consultationroom.setTimetotal(num);
        consultationroomMapper.update(consultationroom,new QueryWrapper<Consultationroom>().eq("consultationRoomId",consultationRoomId));

    }

    @Override
    public void checkTime(Integer id) {
        QueryWrapper<Timeslot> wrapper = new QueryWrapper<Timeslot>().eq("consultationRoomId", id);
        timeslotMapper.delete(wrapper);
        Consultationroom consultationroom = new Consultationroom();
        consultationroom.setTimetotal(0);
        consultationroomMapper.update(consultationroom,new QueryWrapper<Consultationroom>().eq("consultationRoomId",id));
    }


}




